<?php
//require 访问控制类
require_once 'AccessControl.php';
require_once 'Validator.php';

class NoticeController extends Zend_Controller_Action
{

    protected $access;
    
    public function init()
    {
        $this->access = new AccessControl(); //实例化访问控制类
        $this->Validator = new Validator(); //实例化验证类
    }

    public function indexAction()
    {
        // action body
    }

    //添加公告
    public function addAction()
    {
        //是否登录
        if(!$this->access->isLogin())
        {
            $this->_redirect('/User/login');
        }
        
        //是否为管理员
        if(!$this->access->isAdmin())
        {
            $this->view->ErrorInfo = Validator::PermissionDenied;
            return;
        }
        
        $user = $this->access->getSession(); //获取当前用户
        
        $addForm = new Application_Form_NoticeAdd();
        $this->view->addForm = $addForm;
        
        $request = $this->getRequest();
        
        if($request->isPost())
        {
            $post = $request->getPost();
            if($addForm->isValid($post))
            {
                $notice = new Application_Model_Notice();
                $notice->setTitle($post['ntitle']);
                $notice->setContent($post['ncontent']);
                $notice->setUser($user);
                $notice->setTime(date( 'Y-m-d H:i:s', time()));
                
                $noticeDb = new Application_Model_DbTable_Notice();
                $noticeDb->addNotice($notice);
                
                $this->view->ErrorInfo = Validator::AddSuccess;
            }
        }
        
    }

    //查看公告
    public function viewAction()
    {
        $noticeDb = new Application_Model_DbTable_Notice();
        $this->view->notices = $noticeDb->getNotices();
    }

    //更改公告
    public function updateAction()
    {
        //是否为管理员
        if(!$this->access->isAdmin())
        {
            $this->view->ErrorInfo = Validator::PermissionDenied;
            return;
        }
        
        $nid = $this->getRequest()->getUserParam('nid');
        $notice = $this->Validator->isNidValid($nid);
        
        //判断是否传入公告ID和相应公告是否存在
        if($notice == false)
        {
            $this->view->ErrorInfo = Validator::NidError;
            return;
        }
        
        $updateNotice = new Application_Form_NoticeAdd();
        $updateNotice->ntitle->setValue($notice->getTitle());
        $updateNotice->ncontent->setValue($notice->getContent());
        
        $this->view->access = $this->access->isAdmin(); //确认公告存在并允许访问
        $this->view->lastupdate = $notice;
        $this->view->updateNotice = $updateNotice;
        
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            if ($updateNotice->isValid($post))
            {
                $user = $this->access->getSession(); //获取操作的当前用户
                //更新公告字段
                $notice->setTitle($post['ntitle']);
                $notice->setContent($post['ncontent']);
                $notice->setTime(date('Y-m-d H:i:s', time()));
                $notice->setUser($user);
                
                $noticeDb = new Application_Model_DbTable_Notice();
                $noticeDb->updateNotice($notice); //写入数据库
                
                $this->view->ErrorInfo = Validator::UpdateSuccess;
                
            }
        }
    }
    
    //删除公告
    public function deleteAction()
    {
        if (!$this->access->isAdmin())
        {
            $this->view->ErrorInfo = Validator::PermissionDenied;
            return;
        }
        
        $nid = $this->getRequest()->getUserParam('nid');
        
        //判断是否传入公告ID和相应公告是否存在
        if($this->Validator->isNidValid($nid) == FALSE)
        {
            $this->view->ErrorInfo = Validator::NidError;
            return;
        }
        
        $noticeDb = new Application_Model_DbTable_Notice();
        $noticeDb->deleteNotice($nid);
        $this->_redirect('/');
    }
    
}

